import pygame
import sys

# 初始化pygame
pygame.init()

# 设置窗口尺寸
WIDTH, HEIGHT = 640, 480
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("移动的方块")

# 设置颜色
WHITE = (255, 255, 255)
RED = (255, 0, 0)

# 设置方块的初始位置和大小
block_pos = [100, 100]
block_size = 50
block_speed = 5

# 游戏主循环
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # 按键检测
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        block_pos[0] -= block_speed
    if keys[pygame.K_RIGHT]:
        block_pos[0] += block_speed
    if keys[pygame.K_UP]:
        block_pos[1] -= block_speed
    if keys[pygame.K_DOWN]:
        block_pos[1] += block_speed

    # 填充背景色
    screen.fill(WHITE)

    # 绘制方块
    pygame.draw.rect(screen, RED, (block_pos[0], block_pos[1], block_size, block_size))

    # 更新屏幕显示
    pygame.display.flip()

    # 控制游戏循环的帧率
    pygame.time.Clock().tick(60)

pygame.quit()
sys.exit()